﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Data.SqlClient;
using System.Collections;

namespace sqxt
{
    public partial class builduser : Form
    {
        public builduser()
        {
            InitializeComponent();
        }
        string privatekey;//密钥
        SqlConnection conn = new SqlConnection(Dbconnect.s1);
        private void builduser_Load(object sender, EventArgs e) //界面加载时执行
        {
            try
            {
                comboBox1.Items.Add("医生");
                comboBox1.Items.Add("护士");
                comboBox1.Items.Add("其他");
                comboBox2.Items.Add("医生");
                comboBox2.Items.Add("护士");
                comboBox2.Items.Add("其他");
                labelX4.Visible = false;
                ArrayList arr = new ArrayList();
                SqlCommand com = new SqlCommand("select empcode from employee ", conn);
                conn.Open();
                SqlDataReader sdr;
                sdr = com.ExecuteReader();
                while (sdr.Read())
                {

                    arr.Add(sdr.GetString(0));
                }
                foreach (string s in arr)
                {
                    comboBox3.Items.Add(s);
                }
                conn.Close();
                sdr.Close();

                conn.Open();
                SqlCommand sc = new SqlCommand("select empcode,empname,empsex,emptel,empposition,quanxiang from employee where empcode=@usename", conn);
                sc.Parameters.AddWithValue("@usename", Mainform.yh);//传给sql语句的参数

                sdr = sc.ExecuteReader();
                sdr.Read();
                if (sdr[5].ToString()!="管理者")
                {
            
                    tabControl1.Tabs[0].Visible = false;
                }
                conn.Close();
            }
            catch (Exception)
            {
                conn.Close();
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)//保存按钮
        {
            if (textBox1.Text == "" && textBox2.Text == "" && textBox5.Text == "" && comboBox1.Text == "" && textBox6.Text == "")
            {
                MessageBox.Show("必要信息填写不完整");
            }
            else
            {
                if (textBox5.Text != textBox6.Text)
                {
                    MessageBox.Show("2次输入的密码必须相同");
                }
                else
                {
                    string s = jm(textBox5.Text, out privatekey);//加密算法
                    SqlDataAdapter sda = new SqlDataAdapter("select * from employee", Dbconnect.s1);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);//填充数据
                    dt.PrimaryKey = new DataColumn[] { dt.Columns["empcode"] };//主键为医生编号
                    DataRow dr;
                    dr = dt.NewRow();
                    dr["empcode"] = textBox1.Text;
                    dr["empName"] = textBox2.Text;
                    dr["usepwd"] = s;
                    dr["miyao"] = privatekey;//密钥，解密用
                    dr["empsex"] = textBox3.Text;
                    dr["emptel"] = textBox4.Text;
                    dr["empposition"] = comboBox1.Text;
                    dr["quanxiang"] = comboBox4.Text;
                    dt.Rows.Add(dr);
                    SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                    sda.Update(dt);//写到数据库

                    //textBox4SqlDataAdapter sda1 = new SqlDataAdapter("select * from employee", Dbconnect.s1);
                    //DataTable dt1 = new DataTable();
                    //sda1.Fill(dt1);
                    //dt1.PrimaryKey = new DataColumn[] { dt1.Columns["empcode"] };
                    //DataRow dr1;
                    //dr1 = dt1.NewRow();
                    //dr1["name"] = textBox1.Text;
                    //dr1["sf"] = "all";
                    //dt1.Rows.Add(dr1);
                    //SqlCommandBuilder scb1 = new SqlCommandBuilder(sda1);
                    //sda1.Update(dt1);
                    this.Close();
                    MessageBox.Show("创建新用户成功");
                }
            }

        }

        #region  解密算法

        static public string jiemi(string s1, string s2)
        {

            try
            {
                byte[] DataToDecrypt = Convert.FromBase64String(s1);
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                byte[] bytes_Public_Key = Convert.FromBase64String(s2);
                RSA.ImportCspBlob(bytes_Public_Key);
                byte[] bytes_Plain_Text = RSA.Decrypt(DataToDecrypt, false);
                UnicodeEncoding ByteConverter = new UnicodeEncoding();
                string str_Plain_Text = ByteConverter.GetString(bytes_Plain_Text);
                return str_Plain_Text;
            }
            catch (CryptographicException e)
            {
                MessageBox.Show(e.ToString());
                return null;
            }
        }

        #endregion
             static public string jm(string str_Plain_Text, out string str_Private_Key)  //加密函数
        {

            str_Private_Key = "";
            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            byte[] DataToEncrypt = ByteConverter.GetBytes(str_Plain_Text);
            try
            {
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                byte[] bytes_Cypher_Text = RSA.Encrypt(DataToEncrypt, false);
                str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
                string str_Cypher_Text = Convert.ToBase64String(bytes_Cypher_Text);
                return str_Cypher_Text;
            }
            catch (CryptographicException e)
            {
                MessageBox.Show(e.ToString());
                return null;
            }
        
        }
        bool b = false;//准备修改操作
        private void tabControl1_SelectedTabChanging(object sender, DevComponents.DotNetBar.TabStripTabChangingEventArgs e)// 选择选项卡时促发
        {
            //if (b)
            //{

                try
                {
                    conn.Open();
                    SqlDataReader sdr;
                    SqlCommand sc = new SqlCommand("select empcode,empname,empsex,emptel,empposition,quanxiang from employee where empcode=@usename", conn);
                    sc.Parameters.AddWithValue("@usename", Mainform.yh);//传给sql语句的参数
                    sdr = sc.ExecuteReader();
                    sdr.Read();
                    {
                       
                        textBox10.Text = sdr[1] is DBNull ? string.Empty : sdr.GetString(1);
                        comboBox6.Text = sdr[2] is DBNull ? string.Empty : sdr.GetString(2);
                        textBox12.Text = sdr[3] is DBNull ? string.Empty : sdr.GetString(3);
                        comboBox2.Text = sdr[4] is DBNull ? string.Empty : sdr.GetString(4);
                        comboBox5.Text = sdr[5] is DBNull ? string.Empty : sdr.GetString(5);
                        comboBox3.Text = sdr[0] is DBNull ? string.Empty : sdr.GetString(0);
                    }
                    conn.Close();
                    sdr.Close();
                  
                }
                catch (Exception e1)
                {
                    
                    conn.Close();
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
            
            
        }

        private void button2_Click(object sender, EventArgs e) //修改按钮
        {

            if (comboBox3.Text == "" && textBox10.Text == "" &&  comboBox2.Text == "")
            {
                MessageBox.Show("必要信息填写不完整");
            }
            else
            {
                try
                {
                    SqlDataAdapter sda = new SqlDataAdapter("select empcode,empname,empsex,emptel,empposition from employee", Dbconnect.s1);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
                    DataRow dr = dt.Rows.Find(comboBox3.Text.Trim());//找出要修改信息的哪一行
                    
                    dr[0] = comboBox3.Text.Trim();
                    dr[1] = textBox10.Text.Trim();
                    dr[2] = comboBox6.Text.Trim();
                    dr[3] = textBox12.Text.Trim();
                    dr[4] = comboBox2.Text.Trim();
                    SqlCommandBuilder scb = new SqlCommandBuilder(sda);

                    sda.Update(new DataRow[] { dr });
                    MessageBox.Show("已成功修改");
                  
                }catch(Exception e1)
                {
                    
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                }
            }









            //try
            //{ 
            //    conn.Open();
            //    SqlCommand da = new SqlCommand("update employee set empid='" + comboBox3.Text.Trim() + "',empcode='" + comboBox3.Text.Trim() + "',empName='" + textBox10.Text.Trim() + "',empsex='" + comboBox6.Text.Trim() + "',emptel='" + textBox12.Text.Trim() + "',empposition='" +comboBox2.Text.Trim()+ "'  " + "where empid='" + Mainform.yh + "'", conn);
                
            //    da.ExecuteNonQuery();
            //    conn.Close();
            //}
            //catch (Exception e1)
            //{
            //    
            //}
        }

        private void buttonX1_Click(object sender, EventArgs e)//修改密码
        {

            #region 把密码解密出来s3是明文的密码
            try
            {

                if (textBox14.Text != textBox15.Text && textBox14.Text == "" && textBox15.Text == "")
                {
                    MessageBox.Show("2次输入密码不一致或密码不能为空");
                }
                else
                {
                    string s1, s2, s3;//    s1是密文，s2是密钥，通过解密算法得到密码s3
                    conn.Open();
                    SqlCommand sc = new SqlCommand("select usepwd,miyao from employee where empcode=@usename", conn);
                    sc.Parameters.AddWithValue("@usename", Mainform.yh);
                    SqlDataReader sdr = sc.ExecuteReader();
                    sdr.Read();
                    {
                        s1 = sdr.GetString(0);
                        s2 = sdr.GetString(1);
                    }
                    conn.Close();
                    sdr.Close();
                    s3 = jiemi(s1, s2);
            #endregion

                    if (s3 != textBox13.Text.Trim())
                    {
                        labelX4.Visible = true;
                        textBox13.Text = "";
                        textBox13.Focus();
                    }
                    else
                    {
                        conn.Open();
                        string s = jm(textBox14.Text, out privatekey);
                        SqlCommand da = new SqlCommand("update employee set usepwd='" + s + "',miyao='" + privatekey + "'   where empid='" + Mainform.yh + "'", conn);
                        da.ExecuteNonQuery();
                        conn.Close();
                        MessageBox.Show("修改密码成功，下次登录清用新密码登录");
                    }
                }
            }
            catch (Exception e1)
            {
                
            }
            finally
            {
                if(conn.State==ConnectionState.Open)
                    {
                        conn.Close();
                    }
            }
        }

        private void comboBox3_TextChanged(object sender, EventArgs e)//修改时，选择工号发生
        {
            try
            {
                conn.Close();
                conn.Open();
                SqlDataReader sdr;
                SqlCommand sc = new SqlCommand("select empcode,empname,empsex,emptel,empposition,quanxiang from employee where empcode=@usename", conn);
                sc.Parameters.AddWithValue("@usename",comboBox3.Text);//传给sql语句的参数

                sdr = sc.ExecuteReader();
                sdr.Read();
                {
                    //comboBox3.Text = sdr[0] is DBNull ? string.Empty : sdr.GetString(0);
                    comboBox6.Text = sdr[2] is DBNull ? string.Empty : sdr.GetString(2);
                    textBox12.Text = sdr[3] is DBNull ? string.Empty : sdr.GetString(3);
                    comboBox2.Text = sdr[4] is DBNull ? string.Empty : sdr.GetString(4);
                    comboBox5.Text = sdr[5] is DBNull ? string.Empty : sdr.GetString(5);
                    textBox10.Text = sdr[1] is DBNull ? string.Empty : sdr.GetString(1);
                }
                conn.Close();
                sdr.Close();

            }
            catch (Exception e1)
            {
                
                conn.Close();
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
        

       

    }
}